<template>
	<view>
		<scroll-view scroll-y="true" :style="{height: scrollHeight}" @scroll="scrollTop"
			@scrolltolower="$emit('scrolltolower')">
			<view class="" style="padding-bottom: 160rpx;">
				<view class="" v-if="banner.length" style="padding: 10rpx 0 20rpx 0;">
					<u-swiper :list="banner" keyName="pic" previousMargin="30" nextMargin="30" circular autoplay
						radius="5" bgColor="#ffffff" indicator height="320rpx" indicatorMode="line"
						@click="navDetails_"></u-swiper>
				</view>
				<view class="title_" style="margin:20upx 30upx 0 30upx;" v-if="titleText">
					<view class="border_"></view>
					<view class="" style="flex: 1;">
						{{titleText}}
					</view>

					<!-- 		<view class="" style="float: right;color: #999999;">
						更多>
					</view> -->
				</view>

				<view class="goods" :id="index?'':'goods'" v-for="(item ,index) in goodsList_" :key="index"
					@click="navDetails(item)" :style="{backgroundColor: backgroundColor,margin:marginNum}">
					<view class="media">
						<image v-if="Xindex!==index||!item.video_a||!videoShow" class="logo" :src="item.pic" lazy-load>
						</image>
						<video v-if="Xindex==index" autoplay muted :show-fullscreen-btn="false" :src="fastIndex?item.video_c:item.video_a" loop
							controls object-fit="cover" :controls="false" @play="playVideo"></video>

					</view>


					<!-- 			<view class="money" v-if="Xindex!==index&&!item.type==1">
						{{item.price_a?(item.price_s?'会员价：￥'+item.price_s:'会员免费'):'限时免费'}} <text>￥{{item.price}}</text>
					</view> -->

					<view class="text">
						<view class="goods_title">
							<view class="">
								{{item.title}}
							</view>
							<view class="">
								{{item.desc}}
							</view>
						</view>
						<view class="collect" @click.stop="collect(index)">
							<u-icon name="heart" size="40rpx" v-if="item.collect_zt==0"></u-icon>
							<u-icon name="heart-fill" size="40rpx" color="#FF0000" v-else></u-icon>
							收藏
						</view>
					</view>

				</view>
			</view>


			<u-empty v-if="!goodsList_.length" mode="data" icon="http://cdn.uviewui.com/uview/empty/data.png"
				marginTop="200rpx">
			</u-empty>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		props: {
			scrollHeight: {
				type: String,
				default: '100vh'
			},
			backgroundColor: {
				type: String,
				default: '#FAFAFA'
			},
			goodsList: {
				type: Array,
				default: []
			},
			marginNum: {
				type: String,
				default: ''
			},
			bannerShow: {
				type: Boolean,
				default: false
			},
			titleText: {
				type: String,
				default: ''
			},
			fastIndex: {
				type: Boolean,
				default: false
			}
		},
		data() {
			return {
				domHeight: 0,
				Xindex: -1,
				time: null,
				goodsList_: [],
				videoShow: false,
				userData: {},
				banner: []
			}
		},
		watch: {
			goodsList() {
				this.goodsList_ = this.goodsList
				this.userData = uni.getStorageSync("userData")
				if (this.domHeight) return;
				const query = uni.createSelectorQuery().in(this);
				let fn = () => {
					query.select('#goods').boundingClientRect(data => {
						if (data) {
							this.domHeight = data.height
						} else {
							setTimeout(() => {
								fn()
							}, 500)
						}
					}).exec();
				}
				fn()
			}
		},
		mounted() {
			this.goodsList_ = this.goodsList
			if (!this.bannerShow) return;
			let url='home/index/banner'
			if(this.fastIndex)url='home/index/play_banner'
			 this.$myRuquest(url).then(res => {
				this.banner = res.data
			})
		},
		methods: {
			playVideo() {
				this.videoShow = true
			},
			collect(index) {
				let goodsList = this.goodsList_
				let collect_id = goodsList[index].id
				this.$myRuquest('home/user/collect_save', {
					type: goodsList[index].type || 1,
					collect_id
				}).then(res => {
					uni.showToast({
						title: res.msg,
						icon: 'none'
					});

					if (res.code == 200) {
						let item = this.goodsList_[index]
						this.$set(item, 'collect_zt', item.collect_zt == 0 ? 1 : 0)

					}

				})
			},
			scrollTop(e) {
				if (this.time) clearTimeout(this.time)
				this.time = setTimeout(() => {
					if (e.detail.scrollTop < 10) return this.Xindex = 0
					if (~~(e.detail.scrollTop / this.domHeight) !== this.Xindex - 1) {
						this.videoShow = false
						this.Xindex = ~~(e.detail.scrollTop / this.domHeight) + 1
					}
				}, 1000)
			},
			navDetails_(index) {

				let item = this.banner[index]
				let path = 'goodsDetails'
				if (item.show_zt == 2) {
					path = 'textDetails'
				}

				uni.navigateTo({
					url: `/pages/goodsDetails/${path}?id=${item.url_id}&fastIndex=${this.fastIndex}`
				})
			},
			navDetails(item) {
				if (item.type == 99) {
					this.$myRuquest('home/online/online_save', {
						type_id: item.id
					}).then(res => {
						this.nav(res.data.online.id)
					})
				} else {
					let path = 'goodsDetails'
					if (item.type == 2) {
						path = 'textDetails'
					}
					console.log('??', this.fastIndex)
					uni.navigateTo({
						url: `/pages/goodsDetails/${path}?id=${item.id}&fastIndex=${this.fastIndex}`
					})
				}

			},

			nav(onlineId) {
				let path = 'challengeing'
				if (this.type_id == 2) path = 'challengeingVideo'
				uni.navigateTo({
					url: `/pages/challengeList/challengeing/${path}?onlineId=${onlineId}`
				})

			}
		}
	}
</script>

<style lang="scss" scoped>
	.money {
		font-size: 28upx;
		font-weight: 700;
		color: #FFCB71;
		position: absolute;
		right: 24upx;
		top: 320upx;
		z-index: 1999;

		text {
			margin-left: 10upx;
			text-decoration: line-through
		}
	}

	.goods {

		margin: 20upx 0 40upx 0;
		border-radius: 16upx;
		overflow: hidden;
		position: relative;

		.media {
			height: 388upx;
			position: relative;

			>image {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 388upx;
				margin-bottom: 15upx;
				z-index: 999;
			}

			video {
				position: absolute;
				top: 0;
				left: 0;
				width: 100%;
				height: 388upx;
			}
		}


		.text {
			display: flex;
			padding: 20rpx 20upx 20upx 20upx;

			.goods_title {
				flex: 1;

				view:nth-child(1) {
					font-size: 28upx;
					color: #333333;
					font-weight: 700;
				}

				view:nth-child(2) {
					font-size: 24upx;
					color: #999999;
					margin-top: 9upx;

					overflow: hidden; //溢出内容隐藏
					text-overflow: ellipsis; //文本溢出部分用省略号表示
					display: -webkit-box; //特别显示模式
					-webkit-line-clamp: 2; //行数
					line-clamp: 2;
					-webkit-box-orient: vertical;
				}
			}

			.collect {
				display: flex;
				flex-direction: column;
				// justify-content: center;
				// width: 100upx;
				margin-left: 20upx;
				// align-items: center;
				font-size: 20upx;
				color: #999999;
			}
		}
	}
</style>
